//
//  JDImageToastView.swift
//  funcode
//
//  Created by Wan Jiading on 2016/11/30.
//  Copyright © 2016年 WanJiading. All rights reserved.
//

import UIKit

protocol JDImageToastViewDelegate: NSObjectProtocol {
	func jdImageToastViewDidDismiss(_ imageToastView: JDImageToastView)
}

class JDImageToastView: UIView {

	var delegate: JDImageToastViewDelegate?
	
	public func show() {
		
		SharedApplication.keyWindow!.addSubview(self)
		
		let animGroup = CAAnimationGroup()
		
		//		let scaleAnim = CAKeyframeAnimation(keyPath: "transform.scale")
		//		scaleAnim.values = [1.0, 1.01, 1.0, 0.99, 1.0]
		
		let alphaAnim = CAKeyframeAnimation(keyPath: "opacity")
		alphaAnim.values = [1.0, 0.5, 1.0]
		
		animGroup.animations = [alphaAnim]
		animGroup.duration = 2
		animGroup.repeatCount = MAXFLOAT
		animGroup.isRemovedOnCompletion = true	// 退到后台，再次进入前台时，避免动画消失
		
		self.layer.add(animGroup, forKey: nil)
		
		//
		let tapGR = UITapGestureRecognizer(target: self, action: #selector(tapSelf))
		self.addGestureRecognizer(tapGR)
	}
	
	func tapSelf() {
		self.delegate?.jdImageToastViewDidDismiss(self)
	}
	
	func dismiss() {
		
		self.layer.removeAllAnimations()
		
		self.perform(#selector(removeSelf), with: nil, afterDelay: 0.25)
		
		let scaleAnim = CAKeyframeAnimation(keyPath: "transform.scale")
		scaleAnim.values = [1.0, 1.05, 1.0, 0.4]
		scaleAnim.duration = 0.25
		scaleAnim.repeatCount = 1
		
		scaleAnim.isRemovedOnCompletion = false
		scaleAnim.fillMode = kCAFillModeBoth
		self.layer.add(scaleAnim, forKey: "dismissScale")
		
	}
	
	@objc private func removeSelf() {
		self.removeFromSuperview()
	}

	
	// MARK: - 初始化
	
	private let ScreenMargin: CGFloat                    = 60.0    	  // 屏幕俩边最大边距
	private let TextMargin: CGFloat                      = 15.0    	  // 文本四周最大边距
	private let BackgroundViewMinHeight: CGFloat         = 100.0      // 视图的最小高度
	
	convenience init(_ message: String, _ delegate: JDImageToastViewDelegate?) {
		self.init("toast_slide", message, delegate)
	}
	
	convenience init(_ imageName: String, _ message: String, _ delegate: JDImageToastViewDelegate?) {
		self.init(imageName, "toast_close", message, delegate)
	}
	
	convenience init(_ imageName: String, _ cancelBtnImageName: String, _ message: String, _ delegate: JDImageToastViewDelegate?) {
		self.init()
		self.delegate = delegate
		
		let screenSize = MainScreen.bounds.size
		
		let imageView = UIImageView()
		imageView.image = UIImage(named: imageName)
		imageView.sizeToFit()
		imageView.frame = CGRect(x: 0, y: 10, width: imageView.frame.width, height: imageView.frame.height)
		self.addSubview(imageView)
		
		let messageLabel: UILabel = UILabel()
		messageLabel.numberOfLines = 0
		messageLabel.text = message
		messageLabel.textAlignment = NSTextAlignment.center
		messageLabel.font = UIFont.boldSystemFont(ofSize: 16)
		messageLabel.textColor = UIColor.white
		messageLabel.sizeToFit()
		self.addSubview(messageLabel)
		
		let maxWidth: CGFloat = MainScreen.bounds.size.width - ScreenMargin * 2
		let margin: CGFloat = TextMargin * 2
		let width: CGFloat = maxWidth
		messageLabel.frame = CGRect(x: TextMargin, y: 13 + imageView.frame.maxY, width: maxWidth - margin, height: 0)
		messageLabel.sizeToFit()
		DebugLog(messageLabel.frame)
		
		var height: CGFloat = messageLabel.frame.size.height + TextMargin * 2
		if height < BackgroundViewMinHeight {
			height = BackgroundViewMinHeight
		}
		self.frame = CGRect(x: 0, y: 0, width: width, height: height)
		self.layer.cornerRadius = 6
		self.frame = CGRect(x: (screenSize.width - width) * 0.5, y: (screenSize.height - height - 200), width: width, height: height)
		self.backgroundColor = UIColor.rgb("000000", alpha: 0.65)
		
		// 重新赋值 messageLabel.frame
		messageLabel.frame = CGRect(x: TextMargin, y: messageLabel.frame.origin.y, width: width - TextMargin * 2, height: messageLabel.frame.height)
		
		// 重新赋值 imageView.frame
		imageView.frame = CGRect(x: (self.frame.width - imageView.frame.width) * 0.5, y: 10, width: imageView.frame.width, height: imageView.frame.height)
		
		let cancelBtn = UIButton(type: .custom)
		cancelBtn.setImage(UIImage(named: cancelBtnImageName), for: .normal)
		cancelBtn.sizeToFit()
		cancelBtn.frame = CGRect(x: self.frame.width - cancelBtn.frame.width - 9, y: 9, width: cancelBtn.frame.width, height: cancelBtn.frame.height)
		self.addSubview(cancelBtn)
		cancelBtn.isUserInteractionEnabled = false
		
	}
	
	deinit {
		DebugLogFunc()
	}
}
